﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class reg : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
            divErr.Visible = false;
    }
    protected void submit_Click(object sender, EventArgs e)
    {
        string err = "";
        string username = txtUserName.Text;
        string pass = txtPass.Text;
        string cpass = txtCPass.Text;

        if (String.IsNullOrEmpty(username))
            err = "Tài khoản không được để trống";
        else if (String.IsNullOrEmpty(pass))
            err = "Mật khẩu không được để trống";
        else if (String.IsNullOrEmpty(cpass))
            err = "Mật khẩu xác nhận không được để trống";
        else
            err = regAcc(username, pass, cpass);
        
        divErr.Visible = true;
        lblErr.Text = err;
    }

    public string regAcc(string username, string pass, string cpass)
    {
        string err = "";
        SqlConnection conn = new SqlConnection("DRIVER={MySQL};SERVER=localhost; DATABASE=fswd-scgy;USER=root;PASSWORD=123456; OPTION=3;");

        conn.Open();

        SqlDataAdapter da = new SqlDataAdapter("select * from alluser where LoginName='" + username +"'", conn);
        DataTable dt = new DataTable();
        da.Fill(dt);

        if (dt.Rows.Count == 0)
        {
            SqlCommand cmd = new SqlCommand("Insert into alluser", conn);
            int i = cmd.ExecuteNonQuery();

            if (i != 0)
                err = "Đăng ký thành công!";
        }
        else
        {
            err = "Tài khoản đã tồn tại";
        }

        return err;
    }
}